package com.info.zhiduoduo.admin.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.company.QueryCompanyRelationshipListFromBean;
import com.info.zhiduoduo.admin.model.formbean.company.SaveCompanyRelationshipFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.company.CompanyRelationshipListDto;
import com.info.zhiduoduo.repository.entity.company.CompanyRelationship;
import com.info.zhiduoduo.repository.mapper.company.CompanyRelationshipMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class CompanyRelationshipService extends BaseService {

    @Resource
    CompanyRelationshipMapper companyRelationshipMapper;

    public SmartResult saveCompanyRelationship(BaseSimpleFormBean<SaveCompanyRelationshipFormBean> formBean){
        if(null == formBean || null == formBean.getReqdata() || 0 == formBean.getReqdata().getMainCompanyId()
                || 0 == formBean.getReqdata().getCompanyId() || 0 == formBean.getReqdata().getPayType()){
            return errorParamSmartResult();
        }
        //校验企业是否已经绑定了
        CompanyRelationship companyRelationship = new CompanyRelationship().selectOne(Condition.create().eq("company_id", formBean.getReqdata().getCompanyId()));
        if(null != companyRelationship){
            return smartResult(StatusMsg.StatusMsg_336);
        }
        CompanyRelationship relationship = new CompanyRelationship()
                .setMainCompanyId(formBean.getReqdata().getMainCompanyId())
                .setCompanyId(formBean.getReqdata().getCompanyId())
                .setPayType(formBean.getReqdata().getPayType());
        if(!relationship.insert()){
            return smartResult(StatusMsg.StatusMsg_199);
        }
        return smartResult();
    }

    public SmartResult queryCompanyRelationshipList(BaseSimpleFormBean<QueryCompanyRelationshipListFromBean> formBean){
        if(null == formBean || null == formBean.getReqdata()){
            return errorParamSmartResult();
        }
        String companyName = formBean.getReqdata().getCompanyName();
        Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<CompanyRelationshipListDto> relationshipListDtoList = companyRelationshipMapper.queryCompanyRelationshipList(companyName);
        return smartResult(relationshipListDtoList, page.getTotal(), page.getPages(), page.getPageNum());
    }
}
